package com.jsx.controller;


import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
//得改类名
import com.jsx.model.Zhuce;
import com.jsx.model.User;
import com.jsx.model.Addtest;
import com.jsx.model.Denglu;
import com.jsx.service.UserService;


@Controller
public class UserController {
    @Autowired
    private UserService userService;
    public UserService getUserService() {
        return userService;
    }
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @SuppressWarnings("finally")
    //改了
    @RequestMapping("addzhuce")
    public String add(Zhuce user,HttpServletRequest request){
        try {
//			user.setId(UUID.randomUUID().toString());
            //改动了
            System.out.println(user.getId() + ":::::" + user.getUsername() + ":::::" + user.getPassword()+ ":::::" + user.getSex()+ ":::::" + user.getNianling()+ ":::::" + user.getLove()+ ":::::" + user.getJianjie());
            String str = userService.addInfo(user);
            System.out.println(str);
            request.setAttribute("InfoMessage", str);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("InfoMessage", "注册失败le" + e.getMessage());
        } finally {
            //返回登录界面
            return "login";
        }
    }

    //加一个方法addtest，必须两个都加上才行，否则finally那个地方报错。
    @SuppressWarnings("finally")
    @RequestMapping("addtest")
    public String addtest(Addtest user,HttpServletRequest request){
        try {
//			user.setId(UUID.randomUUID().toString());
            //改动了
            System.out.println(user.getId() + ":::::" + user.getUsername() + ":::::" + user.getBianhao()+ ":::::" + user.getMingcheng()+ ":::::" + user.getJiage()+ ":::::" + user.getBeizhu());
            String str = userService.Addtest1(user);
            System.out.println(str);
            request.setAttribute("InfoMessage", str);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("InfoMessage", "添加失败le" + e.getMessage());
        } finally {
            return "result";
        }
    }





    @RequestMapping("getAll")
    public String getAddInfoAll(HttpServletRequest request){
        try {
            List<User> list = userService.getAll();
            System.out.println("------User_list-----"+list.size());
            request.setAttribute("addLists", list);
            return "Mylisttest";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("InfoMessage", "无数据错误" + e.getMessage());
            return "result";
        }
    }

    @SuppressWarnings("finally")
    @RequestMapping("del")
    public String del(int id,HttpServletRequest request){
        try {
            String str = userService.delete(id);
            request.setAttribute("InfoMessage", str);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("InfoMessage", "删除失败" + e.getMessage());
        } finally {
            return "result";
        }
    }
    //@RequestMapping("modify")
    //public String modify(int id,HttpServletRequest request){
    //try {
    //User user = userService.findById(id);
    //request.setAttribute("add", user);
    //return "modify";
    //} catch (Exception e) {
    //e.printStackTrace();
    //request.setAttribute("InfoMessage", "淇℃伅杞藉叆澶辫触锛佸叿浣撳紓甯镐俊鎭細" + e.getMessage());
    //return "result";
    //}
    //}

    @RequestMapping("Myupdatetest")
    public String modifytest(int id,HttpServletRequest request){
        try {
            Addtest user = userService.findById(id);
            request.setAttribute("add", user);
            return "Myupdatetest";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("InfoMessage", "参数错误" + e.getMessage());
            return "result";
        }
    }



    @SuppressWarnings("finally")
    @RequestMapping("update")
    //这里的user换成user1也好使，刚换的2019-3-1，HttpServletRequest request把JSP界面的所有字段都传递过来了
    public String update(Addtest user,HttpServletRequest request){
        try {
            String str = userService.update(user);
            request.setAttribute("InfoMessage", str);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("InfoMessage", "cuowu" + e.getMessage());
        } finally {
            return "result";
        }
    }
    @RequestMapping("login")
    public String login(Denglu user,HttpServletRequest request,HttpSession httpSession){
        try {
            System.out.println("------login--qian----"+user.getUsername()+","+user.getPassword()+".");
            Denglu loginUser = null;
            loginUser=userService.login1(user);
            if(loginUser!=null){
                request.setAttribute("loginUser", loginUser.getUsername());
                httpSession.setAttribute("guanliyuan", loginUser.getUsername());
                return "tiaozhuan_index";
            }else{
                request.setAttribute("loginUser", "登录失败,账户或者密码错误，请重新输入");
                return "tiaozhuan";
            }
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("InfoMessage", "登录错误" + e.getMessage());
            return "result";
        }
    }

    @RequestMapping("yonghulogin")
    public String yonghulogin(Denglu user,HttpServletRequest request,HttpSession httpSession){
        try {
            System.out.println("------login--qian----"+user.getUsername()+","+user.getPassword()+".");
            Denglu loginUser = null;
            loginUser=userService.loginyonghu(user);
            if(loginUser!=null){
                request.setAttribute("loginUser", loginUser.getUsername());
                httpSession.setAttribute("user", loginUser.getUsername());
                return "index";
            }else{
                request.setAttribute("loginUser", "用户登录失败,账户或者密码错误，请重新输入");
                return "login";
            }
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("InfoMessage", "登录错误" + e.getMessage());
            return "result";
        }
    }

}
